#!/bin/bash 

#@description:该脚本用于检查配置生成配置文件
#
#@author:wzj 20171124
#


#还缺一个配置文件验证配置选项的接口





##############################GLOABL VARIABLE################################
SystemConfFile="./system.conf"
AccessLogPath="./log/access/"
ErrorLogPath="./log/error/"

ConfigList=("[SYSTEM]" "[THREAD]" "[LOG]" "[CMD]")

systemConfList=("IP_ADDRESS=" "PORT=8080" "PROCESS_THREAD_NUM=" "LISTEN_THREAD_NUM=1")
threadConfList=("THREAD_PRIORITY=1" "THREAD_STACK_SIZE=2*1024*1024" "THREAD_SCHEDULE_POLICY=RR" )
logConfList=("ACCESS_LOG_PATH=" "ERROR_LOG_PATH=" "WIRTE_LOG_LEVEL=2")
cmdConfList=("CMD_1=display-system-status" "CMD_2=set-pthread-stack-size-#" "CMD_3=set-pthread-policy-#" "CMD_4=display-system-log"
"CMD_5=set-system-log-level-#" "CMD_6=display-thread-id-#-status" "CMD_7=restart" "CMD_8=quit" "CMD_9=help")


function getThreadConfig()
{
	echo "[ INFO ]CREATE THREAD CONFIG OPTION"
	for((index=0; index < ${#threadConfList[@]}; index++))
	do 
		echo "${threadConfList[$index]}" >> $SystemConfFile
	done
}

function getLogConfig()
{
	echo "[ INFO ]CREATE LOG CONFIG OPTION"
	if [ -d $AccessLogPath ];then 
		echo "[ INFO ]HAS FIND ACCESS LOG PATH: $AccessLogPath"
	else
		mkdir -p $AccessLogPath
		echo "[ INFO ]LOCAL PC MAKE ACCESS LOG PATH:$AccessLogPath"
	fi 
	if [ -d $ErrorLogPath ];then
		echo "[ INFO ]HAS FIND ERROR LOG PATH:$ErrorLogPath"
	else  
		mkdir -p $ErrorLogPath 
		echo "[ INFO ]LOCAL PC MAKE ERROR LOG PATH:$ErrorLogPath"
	fi 
	for((index=0; index < ${#logConfList[@]}; index++))
	do
		case $index in 
			0)
				AccessLogPath="${logConfList[$index]}""$AccessLogPath"
				echo "$AccessLogPath" >> $SystemConfFile
				;;
			1)
				ErrorLogPath="${logConfList[$index]}""$ErrorLogPath"
				echo "$ErrorLogPath" >> $SystemConfFile
				;;
			2)
				echo "${logConfList[$index]}" >> $SystemConfFile
				;;
		esac
	done
}

#@description:
#@para:
#
#
#
function getCmdConfig()
{
	echo "[ INFO ]CREATE CMD CONFIG OPTION"
	for((index=0; index < ${#cmdConfList[@]}; index++))
	do 
		echo "${cmdConfList[$index]}" >> $SystemConfFile
	done 
}



#@description:
#@para:
#
#
#
#
function getSystemConfig() 
{
	if [ -z $1 ]; then 
		echo "[ ERROR ] In Function getSystemConfig Get Para Failure!"
		return  1
	fi 
	index=`expr $1 + 0`
	case $index  in 
		0)
			iplist=`ifconfig | grep -w "inet"`
			ipaddress=`echo $iplist | cut -d " " -f 2`
			if [ -z $ipaddress ]; then 
				echo "[ ERROR ]WE CAN'T GET IPADDRESS"
				return  1
			fi 
			echo "[ INFO ]GET LOCAL PC IPADDRESS:$ipaddress"
		    ipAddressConfig="${systemConfList[$index]}""$ipaddress"
			echo "$ipAddressConfig" >> $SystemConfFile
			;;
		1)
			echo "${systemConfList[$index]}" >> $SystemConfFile
			;;
		2)
			coreNum=`cat /proc/cpuinfo | grep processor | wc -l`
			echo "[ INFO ]LOCAL PC HAS PHYICAL PROCESSOR NUM:$coreNum"
			
			if [ $coreNum -gt 2 ];then 
				coreNum=`expr $coreNum - 2`
			elif [ $coreNum -eq 1 ]; then 
				echo "[ INFO ]CURRENT PC HAS ONE CORE!"
			else 
				echo "[ WARN ]CAN'T GET CPU CORE NUM"
				return  1
			fi
			processWorkerNum="${systemConfList[$index]}""$coreNum"
			echo "$processWorkerNum" >> $SystemConfFile
			;;
		3)
			echo "${systemConfList[$index]}" >> $SystemConfFile
			;;
	esac 
}

#@description:获取本地环境的配置
#
#
#
function getSystemLevelConfig()
{
	local index=0
	for((index; index < ${#systemConfList[@]}; index++))
	do
		case $index in 
			0)	
				echo "[ INFO ]SYSTEM START CREATE SYSTEM CONFIG"
				if [ -f $SystemConfFile ];then 
					getSystemConfig $index
				else
					echo "[ ERROR ]GET SYSTEM CONFIGFILE:$SystemConfFile FAILURE!"
					return  1
				fi 
				;;
			1)
				echo "[ INFO ]SYSTEM START CREATE THREAD CONFIG"
				if [ -f $SystemConfFile ];then 
					getSystemConfig $index
				else
					echo "[ ERROR ]GET SYSTEM CONFIGFILE:$SystemConfFile FAILURE!"
					return  1
				fi 
				;;
			2)
				echo "[ INFO ]SYSTEM START CREATE LOG CONFIG"
				if [ -f $SystemConfFile ];then 
					getSystemConfig $index
				else
					echo "[ ERROR ]GET SYSTEM CONFIGFILE:$SystemConfFile FAILURE!"
					return  1
				fi 
				;;
			3)
				echo "[ INFO ]SYSTEM START CREATE CMD CONFIG"
				if [ -f $SystemConfFile ];then 
					getSystemConfig $index
				else
					echo "[ ERROR ]GET SYSTEM CONFIGFILE:$SystemConfFile FAILURE!"
					return  1
				fi 
				;;
			*)
				echo "[ ERROR ] UNKNOWN SYSTEM CONFIG OPTION!"
				return 1
				;;
		esac 
	done
}





#@description:检查系统配置文件
#
#
#
function CheckSystemConfigFile()
{
	local i=0
	if [ -f $SystemConfFile ]; then
		echo "[ INFO ]LOCAL PC HAS FIND CONFIGFILE:$SystemConfFile"
		#you should check configfile is not corret;
	else
		touch $SystemConfFile 
		echo "[ INFO ]LOCAL PC CREATE A NEW CONFIGFILE:$SystemConfFile"
		for((i=0; i < ${#ConfigList[@]}; i++))
		do 
			case $i in 
				0)
					echo "${ConfigList[$i]}" >> $SystemConfFile
					getSystemLevelConfig
					if [ $? -ne  0 ];then 
						echo "[ ERROR ]LOCAL PC GET ENV CONFIGURE OPERATION FAILURE!"
						return 1
					fi
					;;
				1)
					echo "${ConfigList[$i]}" >> $SystemConfFile
					getThreadConfig 
					if [ $? -ne  0 ];then 
						echo "[ ERROR ]LOCAL PC GET ENV CONFIGURE OPERATION FAILURE!"
						return 1
					fi
					;;
				2)
					echo "${ConfigList[$i]}" >> $SystemConfFile
					getLogConfig
					if [ $? -ne  0 ];then 
						echo "[ ERROR ]LOCAL PC GET ENV CONFIGURE OPERATION FAILURE!"
						return 1
					fi
					;;
				3)
					echo "${ConfigList[$i]}" >> $SystemConfFile
					getCmdConfig
					if [ $? -ne  0 ];then 
						echo "[ ERROR ]LOCAL PC GET ENV CONFIGURE OPERATION FAILURE!"
						return 1
					fi
					;;
			esac 
		done 
	fi
}

CheckSystemConfigFile 

